<html>
<head><meta charset="utf-8"><title>optimizing and partial paths · t-compiler/wg-rfc-2229 · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/index.html">t-compiler/wg-rfc-2229</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html">optimizing and partial paths</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="215764255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215764255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215764255">(Nov 05 2020 at 19:44)</a>:</h4>
<p>So <span class="user-mention" data-user-id="281950">@Aman Arora</span> the idea that I was kicking around at the end of the meeting was that maybe instead of doing a "naive desugaring" for closures, we <em>might</em> want to consider skipping to the more "optimized" form, even though that will require more edits to the borrow checker</p>



<a name="215764269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215764269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215764269">(Nov 05 2020 at 19:44)</a>:</h4>
<p>I'm of mixed minds about it</p>



<a name="215764293"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215764293" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215764293">(Nov 05 2020 at 19:44)</a>:</h4>
<p>but the optimized form I'm talking about would be that we only ever carry a (raw) pointer to the local variable</p>



<a name="215764306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215764306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215764306">(Nov 05 2020 at 19:44)</a>:</h4>
<p>and then we just 'know' that we only use some parts of it</p>



<a name="215764362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215764362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215764362">(Nov 05 2020 at 19:45)</a>:</h4>
<p>I guess this only ever applies to by-ref captures</p>



<a name="215764423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215764423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215764423">(Nov 05 2020 at 19:45)</a>:</h4>
<p>tbh I would rather prefer to do the naive mechanism at first and only consider this more optimized form if we had examples to work from (and I think I'd like it better as a MIR <em>optimization</em>, even, though that might be challenging to achieve)</p>



<a name="215764454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215764454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215764454">(Nov 05 2020 at 19:46)</a>:</h4>
<p>but I was wondering if this approach would solve some problems in some way</p>



<a name="215764532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215764532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215764532">(Nov 05 2020 at 19:46)</a>:</h4>
<p>because e.g. we'd still be able to translate <code>convert_var</code> and have it always yield a variable just like it does today</p>



<a name="215765189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215765189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215765189">(Nov 05 2020 at 19:51)</a>:</h4>
<p>I think I kind of understand it. I will think about it tonight, but I guess it would also kind of come down to which one kind of is less hacky. Essentially more work right now might save us debugging (and rewriting code) later on for some possible case we didn't consider in the current planning phase.</p>



<a name="215766181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215766181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215766181">(Nov 05 2020 at 19:58)</a>:</h4>
<p>Yeah. I'm not super keen on having the borrow checker "special case" closures</p>



<a name="215766195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215766195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215766195">(Nov 05 2020 at 19:59)</a>:</h4>
<p>which would be required in the plan I was sketching above</p>



<a name="215766216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215766216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215766216">(Nov 05 2020 at 19:59)</a>:</h4>
<p>and I'm not sure how <code>move</code> captures would be handled either</p>



<a name="215766374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215766374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215766374">(Nov 05 2020 at 20:00)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/optimizing.20and.20partial.20paths/near/215766181">said</a>:</p>
<blockquote>
<p>Yeah. I'm not super keen on having the borrow checker "special case" closures</p>
</blockquote>
<p>Not sure if this would affect workbeing done for Polonius as well</p>



<a name="215766708"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215766708" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215766708">(Nov 05 2020 at 20:03)</a>:</h4>
<p>it would</p>



<a name="215766738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215766738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215766738">(Nov 05 2020 at 20:03)</a>:</h4>
<p>I think I'm leaning towards the naive model myself</p>



<a name="215766882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215766882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215766882">(Nov 05 2020 at 20:04)</a>:</h4>
<p>Okay over the weekend, we will try read through the code and kind of go over what we discussed today. If you get a chance can u upload the video from today and last week?</p>



<a name="215880521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215880521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215880521">(Nov 06 2020 at 17:49)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> can you upload the videos today, if you get a chance? Thanks</p>



<a name="215882684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215882684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215882684">(Nov 06 2020 at 18:07)</a>:</h4>
<p><a href="https://youtu.be/-8simqmm8t4">https://youtu.be/-8simqmm8t4</a></p>
<div class="youtube-video message_inline_image"><a data-id="-8simqmm8t4" href="https://youtu.be/-8simqmm8t4"><img src="https://i.ytimg.com/vi/-8simqmm8t4/default.jpg"></a></div>



<a name="215882698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215882698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215882698">(Nov 06 2020 at 18:07)</a>:</h4>
<p><span class="user-mention" data-user-id="281950">@Aman Arora</span> <span aria-label="point up" class="emoji emoji-1f446" role="img" title="point up">:point_up:</span></p>



<a name="215882766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/215882766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#215882766">(Nov 06 2020 at 18:08)</a>:</h4>
<p>thnak you <span aria-label="heart" class="emoji emoji-2764" role="img" title="heart">:heart:</span></p>



<a name="217106692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/217106692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#217106692">(Nov 18 2020 at 08:23)</a>:</h4>
<p>So we had discussed a bit about we try be smart about when we check for captured Place as we build a the Partial Path, </p>
<p>The way I was thinking of this was at some point we will convert the HIR-PlaceBuilder to MIR equivalent </p>
<ol>
<li>At least the way I picture it, we can do it when we call <code>PlaceBuilder::into_place</code>,  at that point, we will have all the projections that need to be applied. If the base within Projection builder  is a Local we just do what we today, else:</li>
<li>When into_place is called we convert MIR projections to HIR (stop once we see a projection other than Deref, Field or Downcast)</li>
<li>This is when we check the list to find an ancestor (there is going to always be at most 1). I don't see a better way other than linear search here other than storing HIR projections in a prefix tree. </li>
</ol>
<p>So overall one linear search. Once we know the ancestor we will get an idea of how many projections we have consumed, so we intern only the remaining ones.</p>



<a name="217107293"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/217107293" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#217107293">(Nov 18 2020 at 08:30)</a>:</h4>
<hr>
<p>So I guess more clearly:</p>
<ul>
<li>We might not need a HIR/MIR mode, we can just differentiate b/w Local/UpvarRef as the <code>from</code> or <code>base</code> of the PlaceBuilder</li>
<li>once <code>into_place</code> is called, then we try to figure out which captured path is an ancestor of the current Place being built. There is going to be atmost, one ancestor. This is going to be an O(n) operation.</li>
<li>After this, we just build the place using the remaining projections.</li>
<li>We also store the ClosureKind(Fn/...) in PlaceBuilder so that we can appropriately deref once we are ready to build the final place without needing access to infcx.</li>
</ul>



<a name="217145782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/217145782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#217145782">(Nov 18 2020 at 15:03)</a>:</h4>
<p>this makes a lot of sense, yes</p>



<a name="217146478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing%20and%20partial%20paths/near/217146478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/optimizing.20and.20partial.20paths.html#217146478">(Nov 18 2020 at 15:08)</a>:</h4>
<p>sounds good!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>